package com.neuedu.api;


/**
 * 为了解决  多线程锁的问题， 引入了StringBuilder
 */
public class MyStringBuilder {



    public static void main(String[] args) {

        //创建对象
        //  byte[] value = new byte[16];
//        StringBuilder sbuffer = new StringBuilder();  //默认 16 长度的数组
//        StringBuilder sbuffer = new StringBuilder(32);  //32 长度的数组

        StringBuilder sbuffer = new StringBuilder("Hello Buffer");  //容器  32 长度的数组


        //内容 的长度  length()
        System.out.println("sbuffer.length() = " + sbuffer.length()); //12
        //获取容器的长度
        System.out.println("sbuffer.capacity() = " + sbuffer.capacity()); // 28


        //追加内容 没有产生新的 对象
        sbuffer.append("content");

        System.out.println("sbuffer.length() = " + sbuffer.length()); //19  长度没有超过  容器的上限
//        sbuffer.append("123456789");
        sbuffer.append("1234567894897987987"); //超过容器长度 会扩容
        //获取容器的长度
        System.out.println("sbuffer.capacity() = " + sbuffer.capacity());//28

        //插入元素
        System.out.println("sbuffer = " + sbuffer.toString());
        sbuffer.insert(0,"Start ");
        System.out.println("sbuffer = " + sbuffer.toString());

        //遍历元素
        for (int i = 0; i < sbuffer.length(); i++) {
            System.out.println("sbuffer.charAt("+i+") = " + sbuffer.charAt(i));
        }

        //查找
        System.out.println("sbuffer = " + sbuffer.toString());
        int start = sbuffer.indexOf("Buffer");  //lastIndexOf
        int end =  start + "Buffer".length();

        //删除区间
        sbuffer.delete(start ,end);
        System.out.println("sbuffer = " + sbuffer.toString());

        //替换单个字符
        sbuffer.setCharAt(sbuffer.indexOf("H"),'Q');
        System.out.println("sbuffer = " + sbuffer.toString());


        StringBuilder sBuffer1 = new StringBuilder("abc");
        StringBuilder sBuffer2 = new StringBuilder("abc");

        System.out.println("(sBuffer1 == sBuffer2) = " + (sBuffer1 == sBuffer2));

        //StringBuilder 并没有重写 equals的方法
        System.out.println("sBuffer1.toString().equals(sBuffer2.toString()) = " + sBuffer1.toString().equals(sBuffer2.toString()));

    }
}
